<template>
    <view class="container">
        <div class="box">
            <div class="searchbox">
                <img class="image" src="@/static/search.png" alt="">
                <input class="input" type="text" v-model="searchtext" @input="changetext" placeholder="搜索客户名称">
            </div>
            <div class="contentbox">
                <div class="choosemsginfo">
                    <div>
                        <span class="blue mr">保险人数列表</span><span>已选中</span><span class="blue">{{
                            islong && longall ? alltotal : chooselen }}</span><span>人</span>
                    </div>
                    <div class="selectall" @click="onselectall">
                        全选
                    </div>
                </div>
                <div v-for="(ele, index) in personlist" :key="index" v-show="ele.userName.includes(searchtext)">
                    <div class="personlistitem">
                        <div class="personcheck">
                            <checkbox color="#d95757" style="transform:scale(0.7)"
                                :checked="personarr.indexOf(ele) === -1 ? false : true" class="round red"
                                @click="changeselect(ele)" />
                        </div>
                        <div class="personinfo">
                            <div class="personnameinfo">
                                <div class="name">{{ ele.userName }}</div>
                                <div class="idnum">{{ ele.idCardNo }}</div>
                            </div>
                            <div class="lablebox">
                                <div class="lable">保险方案：</div>
                                <div class="content" v-show="type === '长期保'">{{ ele.companyPlanName }}</div>
                                <div class="content" v-show="type === '日日保'">{{ ele.comboName }}</div>
                            </div>
                            <div class="lablebox">
                                <div class="lable">投保时间：</div>
                                <div v-show="type === '长期保'" class="content">{{ ele.insurStartTime }}至{{
                                    ele.insurEndTime }}
                                </div>
                                <div v-show="type === '日日保'" class="content">{{ ele.insureDay }}</div>
                            </div>
                        </div>
                    </div>
                    <div class="linebox">
                        <div class="line"></div>
                    </div>
                </div>
            </div>
        </div>
    </view>
    <view class="btnbox" @click="file">
        <view class="btn">{{ !isTA ? '导出' : '提交申请' }}</view>
    </view>
</template>
<script setup lang="ts">
import { shake } from '@/utils/util'
import { getdayinsurepeopleapi, getlonginsurepeopleapi, getdayinsureexcelapi, getlonginsureexcelapi, getlonginsureexcelapi1 } from '@/api/insureProve'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
onLoad((options: any) => {
    const userInfo = JSON.parse(decodeURIComponent(options.info))
    console.log('userInfo', userInfo.dispatchingAgencyids)
    occupationLevel.value = userInfo.occupationLevel
    companyId.value = userInfo.companyid
    time.value = userInfo.timearr
    level.value = userInfo.level
    type.value = userInfo.type
    insurCompany.value = userInfo.insurecompanycode
    insurCompanyPlanCode.value = userInfo.planCode
    serveCompanyIds.value = userInfo.dispatchingAgencyids
    isDistinct.value = userInfo.RemoveDuplicatesflag
    insureStatus.value = userInfo.underWarrantyflag
    companyPlanName.value = userInfo.companyPlanName
    if (userInfo.insurecompanycode === 'period_012_zdta') {
        isTA.value = true
    }
    if (type.value === '长期保') {
        getlonginsurepeople()
    } else {
        getdayinsurepeople()
    }
})
const isTA = ref(false)
const occupationLevel = ref('')
const companyPlanName = ref('')
const personlist = ref([]) as any
const isDistinct = ref(false)
const insureStatus = ref(false)
const companyId = ref(0)
const insurCompany = ref('')
const insurCompanyPlanCode = ref('')
const time = ref([])
const serveCompanyIds = ref([])
const type = ref('')
const islong = ref(false)
const level = ref(null)
const getlonginsurepeople = async () => {
    console.log(serveCompanyIds.value, 'csjcsjcsjcsjcs')
    const arr = serveCompanyIds.value.join(',')
    console.log(arr, 'csjcsjcsjcsjcs')
    const res = await getlonginsurepeopleapi({ userName: searchtext.value, occupationLevel: occupationLevel.value, isDistinct: isDistinct.value, companyPlanName: companyPlanName.value, policyStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, insurCompanyPlanCode: insurCompanyPlanCode.value, queryStartDate: time.value[0], queryEndDate: time.value[1], ids: arr })
    // let res = await getlonginsurepeopleapi({ isDistinct: isDistinct.value, companyPlanName: companyPlanName.value, policyStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, insurCompanyPlanCode: insurCompanyPlanCode.value, queryStartDate: time.value[0], queryEndDate: time.value[1], ids: arr })
    if (res.length >= 500) {
        islong.value = true
        alltotal.value = res.length
        alllist.value = res
        const reslist = res.slice(0, 501)
        personlist.value = personlist.value.concat(reslist)
    } else {
        islong.value = false
        alltotal.value = res.length
        alllist.value = res
        const reslist = res
        personlist.value = personlist.value.concat(reslist)
    }
    // hasNextPage.value = res.hasNextPage
}
const alllist = ref([]) as any
const getdayinsurepeople = async () => {
    const arr = serveCompanyIds.value.join(',')
    const res = await getdayinsurepeopleapi({ userName: searchtext.value, isDistinct: isDistinct.value, policyStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, planCode: insurCompanyPlanCode.value, beginInsureDay: time.value[0], endInsureDay: time.value[1], ids: arr })
    console.log(res, '121212121212121')
    if (res.length >= 500) {
        islong.value = true
        alltotal.value = res.length
        alllist.value = res
        const reslist = res.slice(0, 501)
        personlist.value = personlist.value.concat(reslist)
    } else {
        islong.value = false
        alltotal.value = res.length
        alllist.value = res
        const reslist = res
        personlist.value = personlist.value.concat(reslist)
    }
    // hasNextPage.value = res.hasNextPage
}
const alltotal = ref(0)
const changeselect = (ele: any) => {
    console.log(ele)
    if (personarr.value.indexOf(ele) == -1) {
        personarr.value.push(ele)
    } else {
        personarr.value.splice(personarr.value.indexOf(ele), 1)
    }
    chooselen.value = personarr.value.length
    console.log(personarr.value)
}
const chooselen = ref(0)
const searchtext = ref('')
const changetext = (e: any) => {
    console.log(e)
    shake(() => {
        // hasNextPage.value = true
        // pageNum.value = 1
        personlist.value = []
        if (type.value === '长期保') {
            getlonginsurepeople()
        } else if (type.value === '日日保') {
            getdayinsurepeople()
        }
    }, 300)
}
const personarr = ref([])
const getdayinsureexcel = async () => {
    const res = await getdayinsureexcelapi({ comboName: companyPlanName.value, isDistinct: isDistinct.value, insureStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, planCode: insurCompanyPlanCode.value, beginInsureDay: time.value[0], endInsureDay: time.value[1], serveCompanyIds: serveCompanyIds.value, recordList: islong.value && longall.value ? alllist.value : respeoploarr.value })
    console.log(res)
    const url = "https://test.51zidan.com/testweb/common/download?delete=false&fileName=" + res
    // const url = "http://192.168.1.133:9090/common/download?delete=false&fileName=" + res
    if (typeof res !== 'string') {
        uni.showToast({
            title: res.msg,
            icon: "none"
        })
        return
    }
    uni.downloadFile({
        url,
        success: (data) => {
            if (data.statusCode === 200) {
                uni.saveFile({ // 文件保存到本地
                    tempFilePath: data.tempFilePath, // 临时路径
                    success: function (res) {
                        console.log(res.savedFilePath)
                        setTimeout(() => { // 可要可不要 这里测试一下是否下载成功
                            // 打开文档查看
                            uni.openDocument({
                                showMenu: true,
                                fileType: "pdf",
                                filePath: res.savedFilePath,
                                success: function (res) {
                                    console.log('打开文档成功')
                                },
                                fail: function () {
                                    uni.showModal({
                                        content: '点击确定复制下载连接',
                                        success: function (res) {
                                            if (res.confirm) {
                                                uni.setClipboardData({
                                                    data: url,
                                                    success: res => {
                                                        console.log(res)
                                                        console.log('复制成功')
                                                        uni.getClipboardData({
                                                            success: res => {
                                                                console.log(res)
                                                                uni.showToast({
                                                                    title: '已复制到剪贴板',
                                                                    icon: 'none'
                                                                })
                                                            }
                                                        })
                                                    }
                                                })
                                            } else if (res.cancel) {
                                                console.log('用户点击取消')
                                            }
                                        }
                                    })
                                }
                            })
                        }, 1000)
                    }
                })
            }
        },
        fail: (err) => {
            console.log(err)
            uni.showToast({
                icon: 'none',
                mask: true,
                title: '失败请重新下载'
            })
        }
    })
    // uni.showToast({
    //     title: res.msg,
    //     icon: "none"
    // })
}
const getlonginsureexcel = async () => {
    // let res = await getlonginsureexcelapi({ level: level.value, companyPlanName: companyPlanName.value, insureStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, insurCompanyPlanCode: insurCompanyPlanCode.value, queryStartDate: time.value[0], queryEndDate: time.value[1], insureInfo: islong.value && longall.value ? alllist.value : respeoploarr.value })
    const res = await getlonginsureexcelapi({ companyPlanName: companyPlanName.value, insureStatus: insureStatus.value, companyId: companyId.value, insurCompany: insurCompany.value, insurCompanyPlanCode: insurCompanyPlanCode.value, queryStartDate: time.value[0], queryEndDate: time.value[1], insureInfo: islong.value && longall.value ? alllist.value : respeoploarr.value })
    const url = "https://test.51zidan.com/testweb/common/download?delete=false&fileName=" + res
    // const url = "http://192.168.1.133:9090/common/download?delete=false&fileName=" + res
    // console.log(res, 'gdfhhugdfuhj 申请已提交,请稍后')
    if (typeof res !== 'string') {
        uni.showToast({
            title: res.msg,
            icon: "none"
        })
        return
    }
    uni.downloadFile({
        url,
        success: (data) => {
            if (data.statusCode === 200) {
                uni.saveFile({ // 文件保存到本地
                    tempFilePath: data.tempFilePath, // 临时路径
                    success: function (res) {
                        console.log(res.savedFilePath)
                        setTimeout(() => { // 可要可不要 这里测试一下是否下载成功
                            // 打开文档查看
                            uni.openDocument({
                                showMenu: true,
                                fileType: "pdf",
                                filePath: res.savedFilePath,
                                success: function (res) {
                                    console.log('打开文档成功')
                                },
                                fail: function () {
                                    uni.showModal({
                                        content: '点击确定复制下载连接',
                                        success: function (res) {
                                            if (res.confirm) {
                                                uni.setClipboardData({
                                                    data: url,
                                                    success: res => {
                                                        console.log(res)
                                                        console.log('复制成功')
                                                        uni.getClipboardData({
                                                            success: res => {
                                                                console.log(res)
                                                                uni.showToast({
                                                                    title: '已复制到剪贴板',
                                                                    icon: 'none'
                                                                })
                                                            }
                                                        })
                                                    }
                                                })
                                            } else if (res.cancel) {
                                                console.log('用户点击取消')
                                            }
                                        }
                                    })
                                }
                            })
                        }, 1000)
                    }
                })
            }
        },
        fail: (err) => {
            console.log(err)
            uni.showToast({
                icon: 'none',
                mask: true,
                title: '失败请重新下载'
            })
        }
    })
    // uni.showToast({
    //     title: res.msg,
    //     icon: "none"
    // })
}
const longall = ref(false)
const onselectall = () => {
    if (islong.value == true) {
        longall.value = true
    }
    personarr.value = []
    personlist.value.forEach((ele: any, index: any) => {
        personarr.value.push(ele)
    })
    chooselen.value = personarr.value.length
    console.log(personarr.value)
}
const respeoploarr = ref([])
const file = () => {
    console.log('导出')
    if (chooselen.value <= 0) {
        uni.showToast({
            icon: "none",
            title: "请选择要导出的人员！"
        })
    } else {
        respeoploarr.value = personarr.value
        if (type.value === '长期保') {
            getlonginsureexcel()
        } else {
            getdayinsureexcel()
        }
    }
}
// const hasNextPage = ref(true)
// const pageNum = ref(1)
// const pageSize = ref(100)

// onReachBottom(() => {
//     console.log(111)
//     if (hasNextPage.value === false) {
//         uni.showToast({
//             title: "暂无更多数据",
//             icon: "none"
//         })
//     } else {
//         pageNum.value += 1
//         if (type.value === '长期保') {
//             getlonginsurepeople()
//         } else if (type.value === '日日保') {
//             getdayinsurepeople()
//         }
//     }
// })
</script>
<style lang="scss" scoped>
.mr {
    margin-right: 16rpx;
}

.box {
    background-color: #FAFAFC;
    min-height: 97vh;
    padding: 32rpx 32rpx 160rpx;

    .contentbox {
        width: 100%;
        box-sizing: border-box;
        border-radius: 16rpx 16rpx 16rpx 16rpx;
        background-color: #FFFFFF;
        margin-top: 24rpx;

        .personlistitem {
            width: 100%;
            box-sizing: border-box;
            padding: 32rpx 32rpx 34rpx;
            display: flex;
            align-items: center;

            .personcheck {
                margin-right: 20rpx;
            }

            .personinfo {
                flex: 1;

                .lablebox,
                .personnameinfo {
                    display: flex;
                    align-items: center;
                }

                .personnameinfo {
                    font-size: 32rpx;
                    color: #202536;
                    font-weight: 600;

                    .name {
                        margin-right: 32rpx;
                    }
                }

                .lablebox {
                    margin-top: 24rpx;
                    justify-content: space-between;
                    font-size: 28rpx;

                    .content {
                        color: #202536;
                    }

                    .lable {
                        color: #9497B1;
                    }
                }
            }

        }

        .choosemsginfo {
            width: 100%;
            box-sizing: border-box;
            height: 114rpx;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 32rpx;
            font-size: 24rpx;

            .selectall {
                cursor: pointer;
                color: #5370F4;
            }

            .blue {
                color: #5370F4;
            }
        }
    }

    .searchbox {
        width: 100%;
        box-sizing: border-box;
        height: 84rpx;
        background-color: #FFFFFF;
        border-radius: 44rpx 44rpx 44rpx 44rpx;
        position: relative;

        .image {
            position: absolute;
            left: 20rpx;
            top: 28rpx;
            width: 28rpx;
            height: 28rpx;
        }

        .input {
            padding-left: 60rpx;
            box-sizing: border-box;
            width: 100%;
            height: 100%;
        }
    }
}

.linebox {
    width: 100%;
    box-sizing: border-box;
    padding: 0 32rpx;

    .line {
        box-sizing: border-box;
        background-color: #ccc;
        height: 1rpx;
        opacity: .5;
    }
}

.btnbox {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    height: 167rpx;
    background-color: #FFFFFF;
    padding: 12rpx 30rpx;

    .btn {
        box-sizing: border-box;
        height: 90rpx;
        line-height: 90rpx;
        text-align: center;
        background-color: #3E5FF5;
        border-radius: 44rpx 44rpx 44rpx 44rpx;
        color: #FFFFFF;
        font-size: 32rpx;
    }
}
</style>
